<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="support.*, java.util.*, myclasses.*, java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>More degrees</title>
</head>
<body>
<center>
<h2>Your degrees:</h2><br>
<%	String discipline = request.getParameter("major"); 
	String discIndex = request.getParameter("majorIndex"); 
	String month = request.getParameter("month"); 
	String year = request.getParameter("year"); 
	String gpa = request.getParameter("gpa");
	String title = request.getParameter("title");
	
	Degree currDeg = (Degree)session.getAttribute("currDegree");
	ArrayList<String> majorArr = new ArrayList<String>();
	if(discipline != null && discIndex == null) {
		try {
			Class.forName("org.postgresql.Driver");
		} catch(Exception e) {
			out.println("org.postgresql.Driver Exception. ");
		}
		try {
			String serverName = "localhost:5432";
		    String mydatabase = "ThirdBase";
		    String url = "jdbc:postgresql://" + serverName + "/" + mydatabase; 
		    String username = "SweetAss";
		    String password = "1234";
		    Connection conn = DriverManager.getConnection(url, username, password);
			Statement stmt = conn.createStatement();
	
			//adding the majors to an array
			ResultSet rset = stmt.executeQuery("SELECT name FROM major;");
			
			while(rset.next()) 
				majorArr.add(rset.getString(1));

			rset.close();
			
			//getting the selected major
			if(!majorArr.contains(discipline)) {
				stmt.executeUpdate("INSERT INTO major (name) VALUES ('" + discipline + "');");
				
				//getting index of recently added major
				ResultSet indexRset = stmt.executeQuery("SELECT id FROM major WHERE name = '" + discipline + "';");
				indexRset.next();
				discIndex = indexRset.getString(1);
				indexRset.close();
			}
			
			conn.close();
			stmt.close();
			
		} catch(SQLException e) {
			e.printStackTrace();
			out.println("Connection exception.");
		}
	}

	if(discIndex != null ) {
		currDeg.setDiscipline(discIndex);
		currDeg.setGpa(gpa);
		currDeg.setMonth(month);
		currDeg.setYear(year);
		currDeg.setName(title);	
	}
	
	//db
	try {
		Class.forName("org.postgresql.Driver");
	} catch(Exception e) {
		out.println("org.postgresql.Driver Exception. ");
	}

	try {
		String serverName = "localhost:5432";
	    String mydatabase = "ThirdBase";
	    String url = "jdbc:postgresql://" + serverName + "/" + mydatabase; 
	    String username = "SweetAss";
	    String password = "1234";
	    Connection conn = DriverManager.getConnection(url, username, password);
	    
		Statement stmt = conn.createStatement();

		Student stud = (Student)session.getAttribute("student");
		if(stud != null) {
			stud.addDegree(currDeg);
			LinkedHashSet<Degree> degs = stud.getDegrees();
			for (Degree d : degs) {
				
				ResultSet rset = stmt.executeQuery("SELECT name FROM location WHERE id = " + d.getLocation());
				rset.next();
				out.println("Location: " + rset.getString(1) + "</br>");
				
				rset = stmt.executeQuery("SELECT name FROM university WHERE id = " + d.getUniversity());
				rset.next();
				out.println("University: " + rset.getString(1) + "<br>");
				
				rset = stmt.executeQuery("SELECT name FROM major WHERE id = " + d.getDiscipline());
				rset.next();
				out.println("Discipline: " + rset.getString(1) + "</br>");				
				
				rset = stmt.executeQuery("SELECT name FROM title WHERE id = " + d.getName());
				rset.next();
				out.println("Title: " + rset.getString(1) + "</br></br>");
				rset.close();
			}
		}
		
		stmt.close();
    	conn.close();
    
	} catch(SQLException e) {
		e.printStackTrace();
		out.println("Connection exception.");
	}
	

%>

<center>
<h4>Submit another degree?</h4>
<form action="provideDegreesChooseLocation.jsp">
<input type="submit" value="Yes"/>
</form>
<form action="specialization.jsp">
<input type="submit" value="No, I'm done!"/>
</form>
</center>
</body>
</html>